package package1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class TestForFileCompare {

	public static void main(String[] args) throws IOException {

		String f1 = "c:/users/alvin.wang/desktop/filepath01.txt";
		// f1 = "c:/users/alvin.wang/desktop/System.getProperties from
		// Eclipse.txt";
		String f2 = "c:/users/alvin.wang/desktop/filepath02.txt";
		// f2 = "c:/users/alvin.wang/desktop/System.getProperties from cmd.txt";
		// createSystemPropertiesFiles(f1,f2);
		// fileCompare(f1,f2);
		fileCompareByMap(f1, f2);

	}

	@SuppressWarnings("unused")
	private static void fileCompareByMap(String f1, String f2) throws IOException {
		Map<String, String> m1 = new HashMap<String, String>();
		Map<String, String> m2 = new HashMap<String, String>();
		File ff1 = new File(f1);
		File ff2 = new File(f2);
		readFileToMap(f1, m1);
		readFileToMap(f2, m2);
		String tab = "\t";
		for (String a : m1.keySet()) {
			if (m2.containsKey(a)) {
				// System.err.println(a+tab+m1.get(a)+tab+m2.get(a));
				compareInDifferentLine(a + tab + m1.get(a), a + tab + m2.get(a));
			} else
				compareInDifferentLine(a + tab + m1.get(a), a + tab);
		}

		Set<String> s1 = m1.keySet();
		Set<String> s2 = m2.keySet();
		s2.removeAll(s1);
		for (String a : s2) {
			compareInDifferentLine(a + tab, a + tab + m2.get(a));
		}

	}

	private static void readFileToMap(String f1, Map<String, String> m1) throws IOException {
		BufferedReader br1 = new BufferedReader(new FileReader(f1));
		String b1;
		while ((b1 = br1.readLine()) != null) {
			m1.put(b1.split("#")[0].trim(), b1.split("#")[1].trim());
		}
		br1.close();
	}

	@SuppressWarnings("unused")
	private static void createSystemPropertiesFiles(String f1, String f2) throws IOException {

		// create f1
		StringBuffer sb = new StringBuffer();
		Properties map = System.getProperties();
		int max = 0;
		int b = 0;
		for (Object a : map.keySet()) {// get max length of character
			b = a.toString().trim().length();
			if (max < b)
				max = b;
		}
		for (Object a : map.keySet()) {
			String temp = addSpace(a.toString().trim(), max + 1) + "\t:\t" + map.get(a).toString().trim() + "\r\n";
			sb.append(temp);
		}
		writeToFile(f1, sb.toString());

		// create f2

	}

	private static String addSpace(String str, int s) {
		int times = s - str.length();
		StringBuffer sb = new StringBuffer(str);
		for (int i = 0; i < times; i++) {
			sb.append(" ");
		}
		return sb.toString();
	}

	@SuppressWarnings("unused")
	private static void fileCompare(String f1, String f2) throws IOException {
		File a = new File(f1);
		File b = new File(f2);
		BufferedReader br1 = new BufferedReader(new FileReader(f1));
		BufferedReader br2 = new BufferedReader(new FileReader(f2));
		String b1, b2;
		compareInDifferentLine(a.getName(), b.getName());
		while ((b1 = br1.readLine()) != null) {
			b2 = br2.readLine();
			if (!b1.equals(b2))
				compareInDifferentLine(b1, b2);
		}
		br1.close();
		br2.close();
	}

	private static void compareInDifferentLine(String b1, String b2) {
		System.err.println("1\t" + b1);
		System.err.println("2\t" + b2);
		System.err.println();
	}

	@SuppressWarnings("unused")
	private static void compareInSameLine(String b1, String b2) {
		System.err.println(b1 + "\t\t\t" + b2);
	}

	private static void writeToFile(String file, String str) throws IOException {
		File f = new File(file);
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));
		bw.write(str);
		bw.close();
	}
}
